package com.aiju.cash.ui.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.base.fragment.BaseTitleFragment;
import com.aiju.cash.R;
import com.aiju.cash.ui.multiple_shops.activity.ClerkActivity;
import com.aiju.cash.ui.setting.AboutActivity;
import com.aiju.cash.ui.setting.PaySettingActivity;
import com.aiju.cash.ui.setting.RateActivity;
import com.aiju.cash.ui.setting.StoreActivity;
import com.aiju.cash.ui.setting.UserActivity;
import com.aiju.common.ImageLoader;
import com.aiju.utils.Util;
import com.aiju.widgit.menu.ListMenuGroup;
import com.aiju.widgit.menu.ListMenuItem;
import com.aiju.widgit.menu.ListMenuView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-11-15 11:05
 * @version: V1.0
 * @Description: 设置界面
 */
public class SettingFragment extends BaseTitleFragment {

    private TextView mNameTextView;

    private ImageView storeImageView;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_setting, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        setCenterTitle("我的");

        inflate.findViewById(R.id.ly_setting_store).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUser.isAdmin()) {
                    startActivity(StoreActivity.class);
                }
            }
        });

        storeImageView = (ImageView) inflate.findViewById(R.id.iv_store_pic);
        mNameTextView = (TextView) inflate.findViewById(R.id.tv_setting_name);
        if (mUser.isAdmin()) {
            inflate.findViewById(R.id.tv_setting_tip).setVisibility(View.VISIBLE);
            inflate.findViewById(R.id.ll_setting_header_right).setVisibility(View.VISIBLE);
        }

        ViewGroup rootView = (LinearLayout) inflate.findViewById(R.id.ly_setting);
        List<ListMenuItem> takeItems = new ArrayList<>();
        takeItems.add(new ListMenuItem(R.mipmap.icon_setting_1, "个人设置", "", R.mipmap.icon_setting_right));
        if (mUser.isAdmin()) {
            takeItems.add(new ListMenuItem(R.mipmap.icon_setting_2, "店员管理", "", R.mipmap.icon_setting_right));
        }
        final ListMenuGroup takeGroup = new ListMenuGroup("", takeItems);
        ListMenuView takeBanner = new ListMenuView(getActivity(), takeGroup, new ListMenuView.OnListMenuClickListener() {
            @Override
            public void onListMenuClick(int position) {
                if (position == 0) {
                    startActivity(UserActivity.class);
                } else if (position == 1) {
                    Intent intent = new Intent(getActivity(), ClerkActivity.class);
                    intent.putExtra("shop", mUser.getShop());
                    startActivity(intent);
                }
            }
        });

        List<ListMenuItem> moneyItems = new ArrayList<>();
        moneyItems.add(new ListMenuItem(R.mipmap.icon_setting_pay, "收款设置", "", R.mipmap.icon_setting_right));
        moneyItems.add(new ListMenuItem(R.mipmap.icon_setting_3, "交易费率", "", R.mipmap.icon_setting_right));
        final ListMenuGroup moneyGroup = new ListMenuGroup("", moneyItems);
        ListMenuView moneyBanner = new ListMenuView(getActivity(), moneyGroup, new ListMenuView.OnListMenuClickListener() {
            @Override
            public void onListMenuClick(int position) {
                if (position == 0) {
                    startActivity(PaySettingActivity.class);
                } else if (position == 1) {
                    startActivity(RateActivity.class);
                }

            }
        });


        List<ListMenuItem> usItem = new ArrayList<>();
        usItem.add(new ListMenuItem(R.mipmap.icon_setting_4, "关于我们", "", R.mipmap.icon_setting_right));
        final ListMenuGroup usGroup = new ListMenuGroup("", usItem);
        ListMenuView usListMenu = new ListMenuView(getActivity(), usGroup, new ListMenuView.OnListMenuClickListener() {
            @Override
            public void onListMenuClick(int position) {
                if (position == 0) {
                    startActivity(AboutActivity.class);
                }
            }
        });

        rootView.addView(takeBanner);
        if (mUser.isAdmin()) {
            rootView.addView(moneyBanner);
        }
        rootView.addView(usListMenu);
    }

    @Override
    public void onResume() {
        super.onResume();
        mNameTextView.setText(mUser.getShop().getShopName());
        if (!Util.isEmpty(mUser.getShop().getImageUrl())) {
            ImageLoader.loadImage(storeImageView, mUser.getShop().getImageUrl());
        }
    }

    @Override
    protected boolean enableDefaultBack() {
        return false;
    }
}
